// 2025/5/11
// 哈夫曼编码

#include <atomic>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct TreeNode
{
    TreeNode(long long _val, TreeNode* _parent = nullptr, TreeNode* _left = nullptr, TreeNode* _right = nullptr)
        :val(_val), parent(_parent), left(_left), right(_right)
    {
    }
    long long val;
    TreeNode* parent;
    TreeNode* left;
    TreeNode* right;
};

struct Compare
{
    bool operator()(TreeNode* t1, TreeNode* t2)
    {
        return t1->val > t2->val;
    }
};

long long ans = 0;
void dfs(TreeNode* root, int deepth)
{
    if (!root)
        return;
    if (!root->left && !root->right)
    {
        ans += root->val * deepth;
        return;
    }
    dfs(root->left, deepth + 1);
    dfs(root->right, deepth + 1);
}

int main() {
    priority_queue<TreeNode*, vector<TreeNode*>, Compare> trees;
    vector<TreeNode> nodes;
    // 注意不要让存放TreeNode的数组扩容，要不然trees里面的结点指针就变野指针了
    nodes.reserve(400000);
    int n; cin >> n;
    long long a;
    while (n--)
    {
        cin >> a;
        nodes.emplace_back(a);
        trees.push(&nodes.back());
    }
    while (trees.size() > 1)
    {
        TreeNode* left = trees.top();
        trees.pop();
        TreeNode* right = trees.top();
        trees.pop();
        nodes.emplace_back(left->val + right->val, nullptr, left, right);
        trees.push(&nodes.back());
    }
    TreeNode* HafTree = trees.top();
    trees.pop();
    dfs(HafTree, 0);
    cout << ans << endl;
}
// 64 位输出请用 printf("%lld")